package cn.com.self.apis.portal.controller;

import cn.com.self.apis.portal.entity.GradEntity;
import cn.com.self.apis.portal.service.intf.IGradService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataList;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description
 */
@Controller
@RequestMapping("/grad")
@Api(description="访问日记模块相关接口")

public class GradController {

    @Autowired
    private IGradService gradService;

     /**新增班级
      * @Description //TODO 
      * @return 
      **/
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增班级")

    public ResponseEntity create(@RequestBody GradEntity entity, HttpServletRequest request){

        GradEntity gradEntity = this.gradService.create(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(gradEntity)));
    }

     /**获取班级详情
      * @Description //TODO
      * @return
      **/
    @RequestMapping(value = {"/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadUser(@PathVariable("id") String id,HttpServletRequest request) {
        GradEntity sysUserEntity = this.gradService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(sysUserEntity)));

    }
    
     /**分页获取班级
      * @Description //TODO 
      * @return 
      **/
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getUserPage(HttpServletRequest request, Pageable page){
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        ICustomPage<GradEntity> pageData = this.gradService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));

    }

     /**获取班级列表
      * @Description //TODO 
      * @return 
      **/
     @RequestMapping(value = "/list")
     @ResponseBody
    public ResponseEntity getGradList(HttpServletRequest request, Sort sort){
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
       List<GradEntity> pageData = this.gradService.list(queryParams,sort);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataList(pageData)));

    }

     /**修改班级
      * @Description //TODO 
      * @return 
      **/

    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody GradEntity entity, HttpServletRequest request) {
        GradEntity gradEntity = this.gradService.load(id);
        CopyUtils.copyProperties(entity, gradEntity);
        GradEntity info = this.gradService.update(gradEntity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(info)));

    }


    /**删除班级
     * @Description //TODO
     * @return
     **/
    @ResponseBody
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResponseEntity deletes(@PathVariable("id") String id){
        this.gradService.delete(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

}
